package com.ptbuy.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.ptbuy.R;
import com.ptbuy.db.pojo.ProductPojo;
import com.ptbuy.utils.Utils;

import java.io.File;
import java.math.BigDecimal;
import java.util.List;

public class ProductAdapter extends ArrayAdapter<ProductPojo> {

    private Context context;

    private  List<ProductPojo> items;

    public ProductAdapter(Context context, List<ProductPojo> items) {
        super(context, 0, items);
        this.context = context;
        this.items = items;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_product_item, parent, false);
        }
        ProductPojo item = getItem(position);

        LinearLayout priLayout = convertView.findViewById(R.id.priLayout);


        TextView myOrder = convertView.findViewById(R.id.myOrder);//编号
        myOrder.setText((items.size() - position) + "");
        TextView exportPath = convertView.findViewById(R.id.itemTitle);

        exportPath.setText(item.getProductName());

        boolean b = item.getPrimitiveProductPrice() != null;
        if (b) {
            priLayout.setVisibility(View.VISIBLE);

            TextView itemSumPri = convertView.findViewById(R.id.itemSumPri);
            String primitiveProductPrice = item.getPrimitiveProductPrice();

            itemSumPri.setText(Utils.getNumlStr(primitiveProductPrice));
        } else {
            priLayout.setVisibility(View.GONE);
        }

        TextView exportDate = convertView.findViewById(R.id.itemSum);
        String productPrice = item.getProductPrice();

        String numlStr = Utils.getNumlStr(productPrice);

        exportDate.setText(numlStr+"元");


        ImageView productImg = convertView.findViewById(R.id.productImg);

        convertView.findViewById(R.id.itemRemove).setOnClickListener(v -> {
            //删除
            opt(1, item);
        });


        convertView.findViewById(R.id.itemModify).setOnClickListener(v -> {
            //修改
            opt(3, item);
        });

        Button itemView = convertView.findViewById(R.id.itemView);
        if (item.getMult() == null || "".equalsIgnoreCase(item.getMult())) {
            productImg.setImageResource(R.drawable.no_image);
        } else {
            List<String> list = JSONObject.parseArray(item.getMult(), String.class);
            String s = list.get(0);
            File file = new File(s);
            if (file.exists()) {
                if (list.get(0).contains(".mp4")) {
                    productImg.setImageResource(R.drawable.video);
                } else {
                    Bitmap bitmap = BitmapFactory.decodeFile(s);
                    productImg.setImageBitmap(bitmap);
                }

            } else {
                productImg.setImageResource(R.drawable.image_loss);
            }
        }

        itemView.setOnClickListener(v -> {
            //查看照片
            opt(2, item);
        });
        return convertView;
    }

    public interface ProductInterface {
        void execute(int type, ProductPojo productPojo);
    }

    private ProductInterface productInterface;

    public void setProductInterface(ProductInterface productInterface) {
        this.productInterface = productInterface;
    }

    private void opt(int type, ProductPojo productPojo) {
        productInterface.execute(type, productPojo);
    }
}
